package com.WebProject.web;

import com.WebProject.control.AdminDao.AdminControlDao;
import com.WebProject.control.AdminDao.Impl.AdminControlDaoImpl;
import com.WebProject.control.UserDao.Impl.UserControlDaoImpl;
import com.WebProject.control.UserDao.UserControlDao;
import com.WebProject.mapper.AdminMapper;
import com.WebProject.pojo.*;
import com.WebProject.service.AdminDao.AadminServiceDao;
import com.WebProject.service.AdminDao.impl.AdminServiceDaoImpl;
import com.WebProject.service.UserDao.UserServiceDao;
import com.WebProject.service.UserDao.impl.UserSerbiceDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

public class CarServlet extends BaseServlet{


    private AdminControlDao mapper = new AdminControlDaoImpl();
    private UserControlDao usermapper = new UserControlDaoImpl();

    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("aa");
        String Car_Number = req.getParameter("car_Number");
        String brand_Name = req.getParameter("brand_Name") ;
        String Model = req.getParameter("model");
        String color = req.getParameter("color");
        String category_Name = req.getParameter("category_Name");
        String t_Comments = req.getParameter("t_Comments");
        String Price =  req.getParameter("price");
        String Rent =  req.getParameter("rent");
        String Status = req.getParameter("status");
        String UseAble = req.getParameter("useAble");

        Integer statusInt;
        Integer useableInt;
        if (Status.equals("未出租")){
            statusInt = 0;
        }else {
            statusInt = 1;
        }

        if (UseAble.equals("可用")){
            useableInt = 0;
        }else {
            useableInt = 1;
        }
        if (action != null && action.equals("add")){
            t_car car = new t_car(null,Car_Number,brand_Name,Model,color,category_Name,t_Comments,
                    Double.valueOf(Price),Double.valueOf(Rent),statusInt,useableInt);
            if (!mapper.addCar(car)){
                req.setAttribute("msg","添加失败，检查添加的车辆是否合规");
            }
        }else {
            t_car car = new t_car(null,Car_Number,brand_Name,Model,color,category_Name,t_Comments,
                    Double.valueOf(Price),Double.valueOf(Rent),null,useableInt);
            Boolean aBoolean = mapper.updateAppointCarInfo(car);
            if (!aBoolean){
                req.setAttribute("msg","修改失败，检查添加的车辆是否合规");
            }
        }

        resp.sendRedirect(req.getContextPath() + "/Admin/CarServlet?action=list");
    }



    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String car_number = req.getParameter("car_number");
        if (!mapper.deleteAppointCar(car_number)){
            req.setAttribute("msg","删除失败,请再次尝试");
        }

        resp.sendRedirect(req.getContextPath() + "/Admin/CarServlet?action=list");
    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String car_number = req.getParameter("car_number");
        t_car car = mapper.queryAppointCarInfo(car_number);
        //???????????????什么玩意????????????????
        req.setAttribute("cars",car);
        //回头再看
        req.getRequestDispatcher("/pages/AdminPage/updateCarInfo.jsp").forward(req,resp);


    }
    protected void addb(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        ArrayList<t_brand> t_brands = mapper.QueryAllBrandInfo();
        ArrayList<t_category> t_categories = mapper.QueryAllCategoryInfo();
        req.setAttribute("brands",t_brands);
        req.setAttribute("categories",t_categories);

        req.getRequestDispatcher("/pages/AdminPage/addCarInfo.jsp").forward(req,resp);
    }


    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        ArrayList<t_car> cars = mapper.QueryAllCarInfo();

        req.setAttribute("cars", cars);

        if (cars == null || cars.isEmpty()){
            req.setAttribute("msg","没有改数据");
        }
        req.getRequestDispatcher("/pages/AdminPage/carList.jsp").forward(req,resp);
    }

    protected void queryZu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ArrayList<t_car_zu> t_car_zus = mapper.queryAppointAllCarInfo();
        req.setAttribute("zus",t_car_zus);

        if (t_car_zus ==null || t_car_zus.isEmpty()){
            req.setAttribute("msg","没有改数据");
        }
        req.getRequestDispatcher("/pages/AdminPage/queryZu.jsp").forward(req,resp);
    }

}
